<?php 
	class Surveys{
		function __construct(){
			FunctionsHelper::includeFiles(
				array(
					ADMINISTRATOR_MODEL_DIRECTORY . 'model.php',
					ADMINISTRATOR_MODEL_DIRECTORY . 'survey.php',
					ADMINISTRATOR_MODEL_DIRECTORY . 'option.php'
				)
			);
		}

		public function getAll(){
			$arrSurveys = array();
			$arrResult = Survey::getAll();
		
			foreach($arrResult as $survey){
				$arrSurveys[] = new Survey($survey);
			}
			return $arrSurveys;
		}
		
		public function getSurvey($arrInitialValue = null){
			if (empty($arrInitialValue))
				return new Survey();
				
			$optionsFromSurvey = array();
		
			$survey = new Survey($arrInitialValue);
			$surveyInformation = $survey->getSurvey();
			$surveyInformation = $surveyInformation[0];
			
			$surveyOptions = $survey->obtainOptions();
			foreach ($surveyOptions as $Option){
				$optionObject = new Option($Option);
				$optionsFromSurvey[] = $optionObject;
			}
			
			$surveyInformation['options'] = $optionsFromSurvey;
			return new Survey($surveyInformation);
					
		}
		
		public function deleteSurvey($surveyId){
			$survey = $this->getSurvey(
					array(
						'survey_id' => $surveyId
					)
			);
			return $survey->delete();
		}
		
		public function createSurvey($arrInitialValues = null){
			if (empty($arrInitialValues) || empty($arrInitialValues['description']))
				return array();
		
			$survey = new Survey($arrInitialValues);
			$lastInsertedId = $survey->create();
			
			$option = new Option();
			foreach ($arrInitialValues['options'] as $value){
				if(!empty($value))
					$option->create($lastInsertedId, $value['description']);
			}
			if ($this->_deactivateIfHasNoOptions($lastInsertedId))
				$survey->deactivateOthers();
			return $lastInsertedId;
		}

		public function updateSurvey($arrSurvey){
			$this->updateOptions($arrSurvey);
			$survey = new Survey($arrSurvey);
			$survey->update();
			if ($this->_deactivateIfHasNoOptions($survey->getSurveyId()))
				$survey->deactivateOthers();
			return 1;
		}
		
		private function _deactivateIfHasNoOptions($surveyId){
			$survey = new Survey();
			if ($survey->deactivateIfHasNoOptions($surveyId)){
				FunctionsHelper::saveErrorInSession('SurveyNotActivated', 'La encuesta no puede ser activada ya que no tiene opciones cargadas.');
				return 1;	
			}
			
			return 0;
		}
		
		public function updateOptions($arrOptions){
			$option = new Option();
			foreach ($arrOptions['options'] as $value){
				$option = new Option($value);
				if($option->getOptionId() == 0)
					$option->create($arrOptions['survey_id'], $option->getDescription());
				else
					$option->update($arrOptions['survey_id']);
			}				
		}
		
		public function activateSurvey($id){
			$survey = new Survey(
				array (
					'survey_id' => $id
				)
			);
			
			$survey->activate();
			if (!$this->_deactivateIfHasNoOptions($id))
				$survey->deactivateOthers();
			return 1;
		}
		
		public function deactivateSurvey($id){
			$survey = new Survey(
				array (
						'survey_id' => $id
				)
			);
		
			return $survey->deactivate();
		}
		
		public function deleteOption($optionId){
			$option = new Option(
				array (
						'option_id' => $optionId
				)
			);
			return $option->delete();
		}
		
		public function createOption($arrInitialValues){
			$option = new Option();
			return $option->create($arrInitialValues['survey_id'], $arrInitialValues['optionDescription']);
		}
	}

?>